<?php

//strlen\mb_strlen字符串长度
$a = '123计算机';
echo strlen($a), "\n";//字节的个数 // 12
echo mb_strlen($a),"\n";//字符的个数 //6
$b = iconv( 'utf-8', 'GBK', $a);
echo strlen($b), "\n"; // 9 字符串显示时的宽度，1个汉字是两个英文字符的宽度
//strcmp用于比较两个字符串的大小
echo strcmp( "Hello",  "Hi"), "\n"; //-1
echo strcasecmp("hi",  "Hi"), "\n"; //0
echo strcasecmp("hi~",  "Hi"), "\n"; //1
//trim删除字符串首尾的空白
$s = "\t\t\t How are you? \t  ";
echo "[$s]\n"; //[		 How are you? 	 ](这里的空白为制表符和空格)
echo "[", trim($s), "]\n"; //[How are you?]
echo "[", ltrim($s), "]\n"; //[How are you? 	 ](右侧仍有空白)
echo "[", rtrim($s), "]\n"; //[		 How are you?](左侧仍有空白)
$s = ",..How are you? __";
echo "[", trim($s, ".,_ "), "]\n"; //[How are you?]
//strpos查找子串在父字符串中的位置，i忽略大小写，r相反的顺序
echo strpos( '计算机程序设计',  '程序');  // 显示 9
echo stripos( "I'm Happy.", 'h');  // 显示 4
echo stripos("How old are you?",  'o');  // 显示 13
var_dump(strpos('计算机程序设计', '小程序'));  //bool(false) 没有找到
//bin2hex将字符串的每个字节依次转换为两个16进制字符
$a='123计算机';
echo bin2hex($a),"\n"; //313233e8aea1e7ae97e69cba
//chunk_split将字符串按指定的长度用指定的字符分隔开来
echo chunk_split(bin2hex($a),2,'_'),"\n"; //31_32_33_e8_ae_a1_e7_ae_97_e6_9c_ba_
//str_replace字符串替换
echo str_replace('o','ooo',"How are you?\n");//Hooow are yooou?
echo str_replace(['o', 'r'],'oo',"How are you?\n");//Hoow aooe yoou?
echo str_replace(['o', 'r'],['O','R'],"How are you?\n");//HOw aRe yOu?
echo str_replace(['o','r',' '],'',"How are you?\n");//Hwaeyu?
print_r(str_replace(['o', 'r'],['O','R'],["How are you?",'So good']));
/*
Array
(
    [0] => HOw aRe yOu?
    [1] => SO gOOd
) */
//str_repeat重复字符串若干次, 0次会返回空字符串""
echo str_repeat('Ok',3),"\n"; // OkOkOk
echo str_repeat('-',60),"\n";
//join 拼接数组元素为一个字符串
echo join(['Hi','Zhangsan']),"\n"; //HiZhangsan
echo join(', ', [123,456,789]),"\n"; //123, 456, 789
//str_repeat重复字符串若干次, 0次会返回空字符串""
echo str_repeat('Ok',3),"\n"; // OkOkOk
echo str_repeat('-',60),"\n";
//join 拼接数组元素为一个字符串
echo join(['Hi','Zhangsan']),"\n"; //HiZhangsan
echo join(', ', [123,456,789]),"\n"; //123, 456, 789
//strtolower、strtoupper转换大小写
echo strtolower("How Are You.\n");//how are you.
echo strtoupper("I'm fine, Thank you.\n"); //I'M FINE, THANK YOU.
//printf、sprintf格式化输出
$a=16.38759;
printf("[%f] [%1$.2f] [%1$6.2f] [%1$-6.2f]\n",$a); //[16.387590] [16.39] [ 16.39] [16.39 ]
$a="How are you?";
printf("[%s] [%1$.3s] [%1$5.3s] [%1$'*6.3s]\n",$a); //[How are you?] [How] [  How] [***How]
$a="计算机";
printf("[%s] [%1$6.3s]\n",$a);//[计算机] [   计]
//substr取子串
echo substr("Hello",0,2),"\n"; //He
echo substr("Hello",0,-2),"\n"; //Hel
echo substr("Hello",-4,-2),"\n"; //el
//str_starts_with是否以指定的串开头
var_dump(str_starts_with('Hello',"He"));//bool(true)
var_dump(str_starts_with('Hello',"he"));//bool(false)
var_dump(str_ends_with('Hello',"o"));//bool(true)

//将数组中的每项数据及其对应的 16 进制串，两端对齐显示在指定的宽度内
//将数组中的每项数据及其对应的 16 进制串，两端对齐显示在指定的宽度内，若宽度不足，则两项数据直接显示。
$a=['张三丰','里  斯','阿 kit','papi 酱','Jackson'];
outFormat(35,$a); //利用函数处理数组数据, 输出内容在35个英文字符宽度内

//完善函数功能，将数组中的每项数据及其16进制串，两端对齐显示在指定的宽度内
//数组中的每一项数据中包含的空格要删除掉
//16进制串中每个字节占两个16进制位，英文字符采用大写，各字节间以_分隔
//本文件存储采用utf-8编码
///* 输出内容如下：
//张三丰   E5_BC_A0_E4_B8_89_E4_B8_B0
//里斯              E9_87_8C_E6_96_AF
//阿kit             E9_98_BF_6B_69_74
//papi酱         70_61_70_69_E9_85_B1
//Jackson        4A_61_63_6B_73_6F_6E
//*/
function outFormat(int $width, ?array $a):void
{
    if (is_null($a)) return;
    foreach ($a as $item) {
        $name = str_replace(' ', '', $item);
        $right = bin2hex($name);
        $right = strtoupper($right);
        $right = chunk_split($right, 2, '_');
        $right = substr($right, 0, -1);
        $rightLength = strlen($right);
        $nameLength = strlen(iconv('utf-8', 'GBK', $name));
        $spaceLength = $width - $nameLength - $rightLength;
        echo $name, str_repeat(' ', $spaceLength), $right, "\n";
    }
}
/* 输出内容如下：
}
张三丰   E5_BC_A0_E4_B8_89_E4_B8_B0
里斯              E9_87_8C_E6_96_AF
阿kit             E9_98_BF_6B_69_74
papi酱         70_61_70_69_E9_85_B1
Jackson        4A_61_63_6B_73_6F_6E
*/


